# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
  #include AuthenticatedSystemBrowse
  #application.rb修改了用户常去的小组不存在时的bug，_grouplist.rhtml在<%%>的后面加了'-'，减少了多输出的空格
  before_filter :login_from_cookie
  
  after_filter :update_current_user_status
  
  def talks_str_of_friend_ids(user)
    friends = user.friends_by_me
    user_ids = friends.collect{|n| n.id if n.is_show_talks < 2}.join(',')
    if user_ids.size <= 0
      user_ids += "#{user.id}"
    else
      user_ids += ",#{user.id}"
    end
  end
  
  def community_tags(community)
    Tag.tags(:limit => 100, :order => "updated_at desc", :type => "common", :geog => "community", :geog_id =>community.id)
  end
  
  def district_tags(district)
    Tag.tags(:limit => 100, :order => "updated_at desc", :type => "common", :geog => "district", :geog_id =>district.id)
  end
  
  def posts_of_same_cat(post)
    if post.class == CommonPost
      posts = CommonPost.find(:all, :conditions => [ "id != ? and post_category_id = ?", post.id, post.post_category_id], :order => "replied_at desc limit 6")
    end
    if post.class == GroupPost
      posts = GroupPost.find(:all, :conditions => [ "id != ? and group_id = ?", post.id, post.group_id], :order => "replied_at desc limit 6")
    end
    if post.class == SitePost
      posts = SitePost.find(:all, :conditions => [ "id != ? and site_id = ?", post.id, post.site_id], :order => "replied_at desc limit 6")
    end
    posts
  end
  
  def get_users_like_groups(users)
    #groups = Array.new
    #users.each do |u|
    #  groups += u.groups
    #end
    #groups.uniq
    if !users.nil? && users.size > 0
      Group.find_by_sql("SELECT groups.* FROM groups INNER JOIN group_users ON groups.id = group_users.group_id WHERE ((group_users.user_id in (#{users.collect{|u| u.id}.join(',')}))) group by groups.id ORDER BY groups.users_count desc limit 0, 9")
    else
       Array.new(0)
    end
    #groups.delete(excluded_group)
  end
  
  def redirect_to_referer(url)
    if request.referer.nil?
      redirect_back_or_default url
    else
      redirect_to request.referer
    end
  end
  
  def pages_for(size, options = {})
    default_options = {:per_page => 15}
    options = default_options.merge options
    pages = Paginator.new self, size, options[:per_page], (params[:page]||1)
    pages
  end
  
  def redirect_post_url(post)
    if post.class == CommonPost
      redirect_to common_post_url(post.community.district, post.community, post)
    elsif post.class == GroupPost
      redirect_to group_post_url(post.group, post)
    elsif post.class == SitePost
      redirect_to site_post_url(post.site, post)
    end
  end
  
  def top_post_category
    @categories = PostCategory.find(:all, :conditions => [ "parent_id = 0"], :order => "position DESC").map{|u| [u.title, u.id]}
  end
  
  def top_post_categories
    @categories = PostCategory.find(:all, :conditions => [ "parent_id = 0"], :order => "position DESC")
  end
  
  def error_page(info)
    pre_html = "<tr><td align=\"left\" valign=\"top\"><p align=\"center\">"
    post_html = "</p></td><td></td></tr>"
    new_info = ""
    new_info << pre_html << info << post_html
    render :text => new_info, :layout => "application"
  end
  
  def rescue_action_in_public(exception)
    logger.error("rescue_action_in_public executed")
    case exception
    when ActiveRecord::RecordNotFound, ::ActionController::RoutingError, ::ActionController::UnknownAction
      logger.error("404 displayed")
      render(:partial  => "shared/e404", :layout => "application", :status   => "404 Not Found")
    else
      logger.error("500 displayed")
      render(:partial  => "shared/e500", :layout => "application", :status   => "500 Error")
    end
  end
  
  def local_request?  
    false  
  end
  
  def user_tags(id = nil)
    tags = Tag.user_tags(:limit => 100, :order => "updated_at desc", :id => id)
  end
  
  def update_current_user_status
    return if session[:user].nil?
    user = User.find(session[:user])
    user.disable_ferret
    user.update_attributes(:last_action_at => Time.now.utc)
    #logger.info "update_current_user_status"
    #logger.info("Time.now = " + Time.now.utc.to_s)
    #logger.info("last_action_at = " + user.last_action_at.to_s)
  end
end
